package com.wgchao.identityrightsmanagement.infrastructure.dao;

import com.wgchao.identityrightsmanagement.domain.organization.model.DepartmentOffice;
import com.wgchao.identityrightsmanagement.domain.organization.repository.DepartmentOfficeRepository;
import com.wgchao.identityrightsmanagement.infrastructure.convert.DepartmentOfficeConvert;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectExistException;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectNotExistException;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseDepartmentOffice;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysBaseDepartmentOfficeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @ClassName: DepartmentOfficeDao
 * @author: wgchao
 * @createTime: 2020/6/17 9:25 AM
 * @Description: TODO
 */
@Component
public class DepartmentOfficeDao implements DepartmentOfficeRepository {
    @Autowired
    private SysBaseDepartmentOfficeRepository departmentOfficeRepository;
    @Autowired
    private DepartmentOfficeConvert departmentOfficeConvert;

    @Override
    public String generateCode() {
        return UUID.randomUUID().toString();
    }

    @Override
    public DepartmentOffice findByDepartmentAndOffice(String departmentCode, String officeCode) {
        SysBaseDepartmentOffice sysBaseDepartmentOffice = departmentOfficeRepository
                .findFirstByDepartmentCodeAndOfficeCode(departmentCode, officeCode);
        if(sysBaseDepartmentOffice == null){
            return null;
        }
        return departmentOfficeConvert.convert(sysBaseDepartmentOffice);
    }

    @Override
    public List<DepartmentOffice> findByDepartment(String departmentCode) {
        List<SysBaseDepartmentOffice> sysBaseDepartmentOffices = departmentOfficeRepository
                .findAllByDepartmentCode(departmentCode);
        if(sysBaseDepartmentOffices==null || sysBaseDepartmentOffices.isEmpty()){
            return null;
        }
        List<DepartmentOffice> departmentOffices = sysBaseDepartmentOffices.stream().map(sysBaseDepartmentOffice -> {
            return departmentOfficeConvert.convert(sysBaseDepartmentOffice);
        }).collect(Collectors.toList());
        return departmentOffices;
    }

    @Override
    public List<DepartmentOffice> findByOffice(String officeCode) {
        List<SysBaseDepartmentOffice> sysBaseDepartmentOffices = departmentOfficeRepository
                .findAllByOfficeCode(officeCode);
        if(sysBaseDepartmentOffices==null || sysBaseDepartmentOffices.isEmpty()){
            return null;
        }
        List<DepartmentOffice> departmentOffices = sysBaseDepartmentOffices.stream().map(sysBaseDepartmentOffice -> {
            return departmentOfficeConvert.convert(sysBaseDepartmentOffice);
        }).collect(Collectors.toList());
        return departmentOffices;
    }

    @Override
    public DepartmentOffice findById(String s) {
        SysBaseDepartmentOffice sysBaseDepartmentOffice = departmentOfficeRepository.findFirstByIdentify(s);
        if(sysBaseDepartmentOffice == null){
            return null;
        }
        return departmentOfficeConvert.convert(sysBaseDepartmentOffice);
    }

    @Override
    public void remove(DepartmentOffice departmentOffice, String operator) {
        SysBaseDepartmentOffice sysBaseDepartmentOffice = departmentOfficeRepository
                .findFirstByIdentify(departmentOffice.getIdentify());
        if(sysBaseDepartmentOffice == null){
            throw new ObjectNotExistException("部门职位对应关系不存在");
        }
        departmentOfficeRepository.delete(sysBaseDepartmentOffice);

        //查询一次 更新删除
        departmentOfficeRepository.findById(sysBaseDepartmentOffice.getId());
    }

    @Override
    public void add(DepartmentOffice departmentOffice, String operator) {
        if(departmentOffice.getId().compareTo(0L) < 0){//新增
            SysBaseDepartmentOffice sysBaseDepartmentOffice = departmentOfficeRepository.findFirstByIdentify(departmentOffice.getIdentify());
            if(sysBaseDepartmentOffice != null){
                throw new ObjectExistException("标识已被占用");
            }
            sysBaseDepartmentOffice = departmentOfficeConvert.convert(departmentOffice);
            sysBaseDepartmentOffice.setId(null);
            sysBaseDepartmentOffice = departmentOfficeRepository.save(sysBaseDepartmentOffice);
        }else{//修改
            SysBaseDepartmentOffice sysBaseDepartmentOffice_old = departmentOfficeRepository.findFirstByIdentify(departmentOffice.getIdentify());
            if(sysBaseDepartmentOffice_old == null){
                throw new ObjectNotExistException("对应关系不存在");
            }
            SysBaseDepartmentOffice sysBaseDepartmentOffice = departmentOfficeConvert.convert(departmentOffice);
            sysBaseDepartmentOffice = departmentOfficeRepository.save(sysBaseDepartmentOffice);
        }
    }

    @Override
    public void addAll(List<DepartmentOffice> departmentOffices, String operator) {
        for(DepartmentOffice departmentOffice : departmentOffices){
            add(departmentOffice, operator);
        }
    }

    @Override
    public void removeAll(List<DepartmentOffice> departmentOffices, String operator) {
        for(DepartmentOffice departmentOffice : departmentOffices){
            remove(departmentOffice, operator);
        }
    }

    @Override
    public void deleteAllByDepartment(String departmentCode) {
        departmentOfficeRepository.deleteAllByDepartmentCode(departmentCode);
        departmentOfficeRepository
                .findAllByDepartmentCode(departmentCode);
    }

    @Override
    public void deleteAllByOffice(String officeCode) {
        departmentOfficeRepository.deleteAllByOfficeCode(officeCode);
        departmentOfficeRepository
                .findAllByOfficeCode(officeCode);
    }
}
